
[HEADER]
	VERSION		01.00.00.00
	DESCRIPTION Iridescence Example
	COPYRIGHT	Imagination Technologies Ltd.
[/HEADER]

[TEXTURES]
	FILE thickness 	Thickness.pvr		LINEAR-LINEAR-NEAREST
[/TEXTURES]

[VERTEXSHADER] 
	NAME 		MyVertexShader 

	[GLSL_CODE]
		attribute highp vec4  inVertex;
		attribute highp vec3  inNormal;
		attribute highp vec2  inTexCoord;
		
		uniform highp mat4  MVPMatrix;
		uniform highp vec3  LightPosition;
		uniform highp vec3  EyePosition;
		
		varying mediump float  CosViewAngle;
		varying mediump float  LightIntensity;
		varying mediump vec2   TexCoord;
		
		void main()
		{
			gl_Position = MVPMatrix * inVertex;
			
			highp vec3 LightDir = normalize(LightPosition - inVertex.xyz);
			
			highp vec3 eyeDirection = normalize(EyePosition - inVertex.xyz);
			
			// Simple diffuse lighting 
			LightIntensity = max(dot(LightDir, inNormal), 0.0);
		
			// Cosine of the angle between surface normal and eye direction
			// We clamp at 0.1 to avoid ugly aliasing at near 90° angles
			CosViewAngle = max(dot(eyeDirection, inNormal), 0.1);
			
			TexCoord = inTexCoord;
		}
	[/GLSL_CODE]
[/VERTEXSHADER]
    
[FRAGMENTSHADER] 
	NAME 		MyFragmentShader 

	[GLSL_CODE]
		uniform sampler2D  sThicknessTex;
		
		uniform highp float  MinThickness;
		uniform highp float  MaxVariation;
		
		varying mediump float  CosViewAngle;
		varying mediump float  LightIntensity;
		varying mediump vec2   TexCoord;
		
		// We use wave numbers (k) for the iridescence effect, given as
		//   k =  2 * pi / wavelength in nm.
		const highp float  PI = 3.141592654;
		const highp vec3   cRgbK = 2.0 * PI * vec3(1.0/475.0, 1.0/510.0, 1.0/650.0);
		
		void main()
		{
			highp float thickness = texture2D(sThicknessTex, TexCoord).r * MaxVariation + MinThickness;
			highp float delta = (thickness / LightIntensity) + (thickness / CosViewAngle);
			lowp vec3 color = cos(delta * cRgbK) * LightIntensity;
			gl_FragColor = vec4(color, 1.0);
		}
	[/GLSL_CODE]
[/FRAGMENTSHADER]
 
[EFFECT] 
	NAME 	Iridescence

	UNIFORM	MVPMatrix		WORLDVIEWPROJECTION
	UNIFORM LightPosition	LIGHTPOSWORLD0
	UNIFORM EyePosition		EYEPOSMODEL

	UNIFORM MinThickness	MINTHICKNESS	float(100.0)
	UNIFORM MaxVariation	MAXVARIATION	float(100.0)

	ATTRIBUTE	inVertex	POSITION
	ATTRIBUTE	inNormal	NORMAL
	ATTRIBUTE	inTexCoord	UV0

	VERTEXSHADER MyVertexShader
	FRAGMENTSHADER MyFragmentShader
	
	TEXTURE 0 thickness

[/EFFECT]
